<?php
require_once './inc/config.inc.php';
require_once './inc/functions.inc.php';

function __autoload($class_name) 
{
    require_once "./lib/".$class_name.".class.php";
}


// @todo: take out this check.  Session should already have been initiated
if (!isset($_SESSION)) {
	session_start();
}
if (isset($_GET['USN'])) {
	$_SESSION['USN'] = $_GET['USN'];
}

// Request of change tripos 
$student = new Student($_SESSION['USN']);

if (isset($_GET['changeTripos'])) {
	print $student->triposForm();
	exit;
}


if (isset($_GET['checkRegistration'])) {
	if ($student->checkRegistration()) {
		print highlightText("You have a valid selection");
	} else {
		print highlightText("You <strong>do not</strong> have a valid selection");
	}
	exit;
}

if (isset($_POST['addchoice'])) {

	if ($response = $student->addChoice($_POST['addchoice'])) {
		print highlightText("Choice added");
	} else {
		header("HTTP/1.0 400 Bad Request");
	}
	exit;
}

if (isset($_POST['removechoice'])) {

	if ($response = $student->removeChoice($_POST['removechoice'])) {
		print highlightText("Choice removed");
	} else {
		header("HTTP/1.0 400 Bad Request");
	}
	exit;
}

if (isset($_POST['status'])) {
	
	if ($response = $student->setStatus($_POST['status'])) {
		$highlights[] = "You're registration has been updated";
	} else {
		$errors[] = "You're registration failed";
	}
}

include_once './inc/header.inc.php';

if (isset($_POST['tripos'])) {
	if ($student->clearChoices()) {
		$student->tripos = $_POST['tripos'];
	} else {
		$errors[] = "Sorry, we hit a problem";
	}
	
}

$tripos = $student->tripos;

$details = $student->getDetails();

$status = $student->getStatus();


?>
<h1>You're logged in as: <?php print  $details["STR_CRSID"]."(".$_SESSION['USN'].")" ?></h1>
<?php 
echo $student->getTableDetails();
?>
<p id="status">Status: <?php print $status ?></p>

<?php

// If not registered but already enroled
if ($tripos === false) {
	print $student->triposForm();
		
// If not registered or enroled
} else {
	
	print $student->choiceForm();
}
?>

<script type="text/javascript" src="js/student.js" />

</body>
</html>
